﻿using Windows.ApplicationModel.Background;

namespace Sumc.Client.ModernUI.Helpers
{
    public static class BackgroundTaskService
    {
        public static BackgroundTaskRegistration RegisterBackgroundTask(
            string taskEntryPoint, string taskName, IBackgroundTrigger trigger, params IBackgroundCondition[] conditions)
        {
            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name == taskName)
                {
                    return (BackgroundTaskRegistration)(cur.Value);
                }
            }

            var builder = new BackgroundTaskBuilder();

            builder.Name = taskName;
            builder.TaskEntryPoint = taskEntryPoint;
            builder.SetTrigger(trigger);

            foreach (var condition in conditions)
            {
                builder.AddCondition(condition);
            }

            BackgroundTaskRegistration task = builder.Register();
            return task;
        }
    }
}
